<template>
  <div class="">
    <!-- 条件筛选-----查询 -->
    <el-form :inline="true">
      <div style="display: flex; justify-content: space-between;">
        <div>
          <el-form-item>
            <el-input v-model="uid" clearable placeholder="用户ID" style="width: 120px;" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="key" clearable placeholder="用户名/姓名" style="width: 550px;" />
          </el-form-item>
          <el-form-item>
            <label>币种：</label>
            <el-select v-model="coinid" placeholder="币种">
              <el-option label="全部" :value="0" />
              <el-option v-for="item in coinList" :disabled="item.coinid == 3" :label="item.coinname"
                :value="item.coinid" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <label>交易类型：</label>
            <el-select v-model="tradetype" placeholder="交易类型" style="width: 180px;">
              <el-option v-for="item in Object.keys(tradetypeList)" :label="tradetypeList[item]" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker v-model="dateTime" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
              start-placeholder="开始日期" end-placeholder="结束日期" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch">查询</el-button>
          </el-form-item>
        </div>
        <div>
          <el-form-item>
            <div class="flex">
              <el-button type="primary" :loading="exportLoading" @click="doExport">导出Excel</el-button>
            </div>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <!-- 列表数据 -->
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
      <el-table-column prop="uid" label="用户ID" align="center" width="80" />
      <el-table-column prop="username" label="用户名" align="center" />
      <el-table-column prop="truename" label="姓名" align="center" />
      <el-table-column prop="income" label="收入金额" align="center" />
      <el-table-column prop="bandkcard" label="银行卡号" align="center" />
      <el-table-column prop="bankname2" label="银行开户行" align="center" />
      <el-table-column prop="bankname" label="银行名称" align="center" />
      <el-table-column label="操作" width="150" fixed="right" align="center">
        <template slot-scope="{row}">
          <el-button size="mini" type="primary"
            @click="$router.push({ name: 'BalanceRecord', query: { userid: row.uid, tradetype: tradetype, coinid: coinid, dateTime: dateTime } })">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="block " style="padding-top: 20px;">
      <el-pagination background :current-page="pageIndex" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="totalCount" @size-change="idx => { pageSize = idx }"
        @current-change="idx => { pageIndex = idx }" />
    </div>
  </div>
</template>

<script>
import { UserBalanceRecordIncomeTotal, UserBalanceRecordIncomeTotalExcel } from '@/api/users.js'
import { getEnumList } from '@/utils/auth.js'
import { GetEnum, coinlist } from "@/api/ctrls.js";
import { formatDate } from '@/utils/index.js'
import digOrserInfo from '../../components/digOrderInfo.vue'
import { mapState } from 'vuex'
export default {
  name: 'Name',
  components: { digOrserInfo },
  data() {
    return {
      dateTime: [], // 时间查询
      uid: '',
      key: '',
      stime: '', // 开始时间 2022-01-01
      etime: '', // 结束时间
      coinid: 0, // 币种ID
      tradetype: '0',
      stype: 0,


      listLoading: false,
      tableData: [],
      totalCount: 0,
      pageIndex: 1,
      pageSize: 20,
      total: {},


      tradetypeList: {},
      coinList: [],

      modalStatus: 0, // 打开组件弹窗参数
      currentInfo: {}, // 当前用户信息
      currentId: 0,

      exportLoading: false, // 导出loading
    }
  },
  computed: {
    // ...mapState({
    //   tradetypeList: state => state.user.enumList["TradeType"]
    // })
  },
  watch: {
    pageIndex(val) {
      console.log(val)
      this.getData()
    },
    pageSize(val) {
      console.log(val)
      this.getData()
    },
    modalStatus(val) {
      this.getData()
    }
  },
  async created() {

  },
  async mounted() {
    this.getData()
    this.GetEnum();
    this.GetCoin();
  },
  methods: {
    // 获取枚举
    async GetEnum() {
      const res = await GetEnum();
      this.tradetypeList = res.data['TradeType']
      // console.log('币种：：', this.coinList);
    },
    // 获取币种
    async GetCoin() {
      const res = await coinlist();
      this.coinList = res;
    },
    /**打开来源/备注弹窗 */
    openOrderInfo(info) {
      this.currentId = info.tid;
      this.modalStatus = 1;
    },
    /////////////////////// 导出报表  ///////////////////////
    // 导出报表
    async doExport() {
      this.exportLoading = true;
      const res = await UserBalanceRecordIncomeTotalExcel({
        uid: this.uid,
        key: this.key,
        coinid: this.coinid,
        stime: this.dateTime ? this.dateTime[0] : '',
        etime: this.dateTime ? this.dateTime[1] : '',
        tradetype: this.tradetype,
      });
      this.exportLoading = false;
      if (res) {
        console.log('导出网络数据：：', res);
        // 文件名
        const fileName = "收入统计列表.xlsx";
        // 处理导出数据并下载
        this.handleExcel(res, fileName);
      }
    },
    /**转换数据类型  下载Excel报表 */
    handleExcel(data, fileName) {
      //转换数据类型
      const xlsx = "application/vnd.ms-excel;charset=utf-8";
      const blob = new Blob([data], { type: xlsx });
      console.log(blob);

      // 转换完成，创建一个a标签用于下载
      const a = document.createElement("a");
      a.style.display = "none";
      a.href = URL.createObjectURL(blob);
      a.setAttribute("download", fileName);
      document.body.appendChild(a);
      a.click();
      a.remove();
    },
    async onSearch() {
      this.pageIndex = 1
      this.getData()
    },
    // 获取数据
    async getData() {
      this.listLoading = true
      const { list, totalCount, _total } = await UserBalanceRecordIncomeTotal({
        uid: this.uid,
        key: this.key,
        coinid: this.coinid,
        stime: this.dateTime ? this.dateTime[0] : '',
        etime: this.dateTime ? this.dateTime[1] : '',
        tradetype: this.tradetype,
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      })
      this.totalCount = totalCount
      this.total = _total
      // list.forEach(element => {
      //   element.createtime = element.createtime.replace('T', ' ')
      //   this.tableData.push(element)
      // })
      this.tableData = list
      this.listLoading = false
    }
  }
}
</script>

<style lang="scss">
.el-table .warning-row {
  background: oldlace;
}
</style>
